﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace robotGUI
{
    public partial class termControl : UserControl
    {
        float[] zooms = { .5f, .6f, .7f, .8f, .9f, 1f, 1.2f, 1.5f };
        ToolStripMenuItem[] zoomItems;

        frmMain.NewMessageEventHandler newMessage;
        public termControl()
        {
            InitializeComponent();
            zoomItems = new ToolStripMenuItem[] {toolStripMenuItem2, toolStripMenuItem3, toolStripMenuItem4, toolStripMenuItem5, toolStripMenuItem6, toolStripMenuItem7, toolStripMenuItem8, toolStripMenuItem9};
        }

        private void termControl_Load(object sender, EventArgs e)
        {
            newMessage = new frmMain.NewMessageEventHandler(termControl_NewMessage);
            logLength = Properties.Settings.Default.logLength;
            txtLines.Text = logLength.ToString();
            txtLog.ZoomFactor = zooms[Properties.Settings.Default.logZoom];
            zoomItems[Properties.Settings.Default.logZoom].Checked = true;
        }

        char[] trimChars = new char[] {' ', '\t', '\n', '\r'};
        public void Append(string text)
        {
            text = text.Trim(trimChars).Trim(trimChars);
            txtLog.Text += "\n" + text;
        }

        void termControl_NewMessage(object sender, NewMessageEventArgs e)
        {
            Append(e.msg.Message);
        }

        int logLength = 0; //Number of lines currently in log
        private void txtLog_TextChanged(object sender, EventArgs e)
        {
            if (logLength != txtLog.Lines.Length) //Lines different?
            {
                //Shorten log
                while (txtLog.Lines.Length > (int)Properties.Settings.Default.logLength)
                    txtLog.Text = txtLog.Text.Substring(txtLog.Text.IndexOf("\n") + 1);
                logLength = txtLog.Lines.Length;
                //Move cursor to end of log
                if (txtLog.Text.Length > 0)
                {
                    try     //Error-prone, just try anyway
                    {
                        txtLog.SelectionStart = txtLog.Text.Length - 1;
                        txtLog.ScrollToCaret();
                    }
                    catch { }
                }
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.logFile == null || Properties.Settings.Default.logFile == "")
                Properties.Settings.Default.logFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.InitialDirectory = Path.GetDirectoryName(Properties.Settings.Default.logFile);
            saveFileDialog1.FileName = "log.txt";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.logFile = saveFileDialog1.FileName;
                txtLog.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.UnicodePlainText);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
        }

        public void Clear()
        {
            float zoom = txtLog.ZoomFactor;
            txtLog.Clear();
            txtLog.ZoomFactor = 1.0F;
            txtLog.ZoomFactor = zoom;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Copy();
        }

        public void Copy()
        {
            txtLog.Copy();
        }

        private void btnZoom_Click(object sender, EventArgs e)
        {
            int zoom;
            foreach (ToolStripMenuItem i in zoomItems) i.Checked = false;
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            int.TryParse((string)item.Tag, out zoom);
            zoomItems[zoom].Checked = true;
            txtLog.ZoomFactor = zooms[zoom];
            Properties.Settings.Default.logZoom = zoom;
        }

        private void chkRaw_Click(object sender, EventArgs e)
        {
            if (chkRaw.Checked)
            {
                chkRaw.Image = Properties.Resources.comments_add;
                if (ParentForm != null)
                    ((frmMain)ParentForm).NewMessage += newMessage;
            }
            else
            {
                chkRaw.Image = Properties.Resources.comments;
                if (ParentForm != null)
                    ((frmMain)ParentForm).NewMessage -= newMessage;
            }
        }

        private void txtLines_TextChanged(object sender, EventArgs e)
        {
            int value;
            if (int.TryParse(txtLines.Text, out value))
            {
                if (value < 1) value = 1;
                if (value > 10000) value = 10000;
                Properties.Settings.Default.logLength = value;
            }
        }

    }
}
